package com.hzf.material.admin.controller.manager;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hzf.common.validator.LengthValidator;
import com.hzf.material.dao.model.MaterialListType;
import com.hzf.material.dao.model.MaterialListTypeExample;
import com.hzf.material.rpc.api.MaterialListTypeService;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe: 市场类别查询
 * creat_user: 白开水
 * creat_time: 2017/11/15   18:04
 **/
@Controller
@Api(value = "材料商商场类型管理", description = "材料商管理")
@RequestMapping("/manage/materialType")
public class MaterialListTypeController {
    private static Logger _log = LoggerFactory.getLogger(MaterialListTypeController.class);

    @Autowired
    private MaterialListTypeService materialListTypeService;


    @ApiOperation(value = "商场类型首页")
    @RequiresPermissions("material:markettype:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/materialType/index.jsp";
    }
    @ApiOperation(value="基础用户列表")
    @RequiresPermissions("material:markettype:read")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        MaterialListTypeExample materialListTypeExample = new MaterialListTypeExample();
        if(!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)){
            materialListTypeExample.setOrderByClause(sort+" "+order);
        }
        List<MaterialListType> rows = materialListTypeService.selectByExampleForOffsetPage(materialListTypeExample,offset,limit);
        long total = materialListTypeService.countByExample(materialListTypeExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增类型")
    @RequiresPermissions("material:markettype:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) { return "/manage/materialType/create.jsp"; }

    @ApiOperation(value = "新增类型")
    @RequiresPermissions("material:markettype:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MaterialListType materialListType){
        ComplexResult result = FluentValidator.checkAll()
                .on(materialListType.getMarketTypeName(), new LengthValidator(1, 50, "类型名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = materialListTypeService.insertSelective(materialListType);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }


    @ApiOperation(value = "删除类型")
    @RequiresPermissions("material:markettype:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = materialListTypeService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "修改类型")
    @RequiresPermissions("material:markettype:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        MaterialListType materialListType = materialListTypeService.selectByPrimaryKey(id);
        modelMap.put("materialListType", materialListType);
        return "/manage/materialType/update.jsp";
    }
    @ApiOperation(value = "修改类型")
    @RequiresPermissions("material:markettype:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, MaterialListType materialListType) {
        ComplexResult result = FluentValidator.checkAll()
                .on(materialListType.getMarketTypeName(), new LengthValidator(1, 50, "类型名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        materialListType.setMarketTypeId(id);
        int count = materialListTypeService.updateByPrimaryKeySelective(materialListType);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }


}
